using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Linq;

namespace Acms.AcmsControls
{
    [ToolboxData("<{0}:HtmlContentControl runat=server></{0}:HtmlContentControl>")]
    public class HtmlContentControlDisplay : BaseContentControlDisplay<HtmlContentControlProperties>
    {
        public override void PropertiesLoaded(HtmlContentControlProperties properties)
        {
            base.PropertiesLoaded(properties);
            if (this.Properties != null)
            {
                Text = this.Properties.GetHtml();
                // Setup the regular expression to find internal links
                //var regExp = new Regex("[[]{2}~/[a-zA-Z0-9//-_]{0,}:[\\sa-zA-Z0-9//-]{0,}]{2}");
                var regExp = new Regex("[[]{2}~/[^:]{0,}:[^]]{0,}[]]{2}");
                // Highlight keywords by calling the delegate each time a keyword is found.
                Text = regExp.Replace(Text, new MatchEvaluator(ReplaceLinks));
            }
        }

        public string Text { get; set; }

        public override void SetDevProperties()
        {
            Text = "<p>Html Content Here</p>";
        }

        protected override void Render(HtmlTextWriter writer)
        {
            RenderBeginTag(writer);
            if (Text != null)
            {
                writer.Write(Highlight(Text));
            }
            RenderEndTag(writer);
        }

        protected override object SaveViewState()
        {
            ViewState["Text"] = Text;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            Text = ViewState["Text"] as string;
        }

        private string Highlight(string text)
        {
            if (text != null)
            {
                Control current = this;
                TemplateUserControl container = null;
                while (container == null && current != null && current.NamingContainer != null)
                {
                    current = current.NamingContainer;
                    container = current as TemplateUserControl;
                }
                if (container != null && !string.IsNullOrEmpty(container.HighlightText))
                {
                    // Setup the regular expression and add the Or operator.
                    Regex regExp = new Regex(string.Concat("\\b", container.HighlightText.Replace(" ", "|").Trim()), RegexOptions.IgnoreCase);
                    // Highlight keywords by calling the delegate each time a keyword is found.
                    return regExp.Replace(text, new MatchEvaluator(ReplaceKeyWords));
                }
                else
                {
                    return text;
                }
            }
            else
            {
                return text;
            }
        }

        private string ReplaceKeyWords(Match m)
        {

            return "<span class=\"searchHighlight\">" + m.Value + "</span>";

        }

        private string ReplaceLinks(Match match)
        {
            var path = match.Value.Substring(2, match.Value.IndexOf(":") - 2);
            var text = match.Value.Substring(match.Value.IndexOf(":") + 1, match.Value.Length - match.Value.IndexOf(":") - 3);

            var post = Bll.Post.GetBySiteIdAndPath(CurrentContext.SiteId, path, CurrentContext.SiteVersion);
            if (post != null)
                return string.Format("<a class=\"inline_link\" href=\"{0}\">{1}</a>", Page.ResolveUrl(post.Url), text);
            else
                return text;
        }

        public override bool IsEmpty
        {
            get { return string.IsNullOrEmpty(Text); }
        }
    }
}
